/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ptone.messagecenter.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ptone.messageCenter.common.enums.Enums;
import com.ptone.messageCenter.common.utils.TimeUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 公共字段，自动填充值
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
    private final static String IS_DEL = "isDel";
    private final static String IS_READ = "isRead";
    private final static String IS_CONFIRM = "isConfirm";
    private final static String CREATE_TIME = "createTime";
    private final static String UPDATE_DATE = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = TimeUtil.getNow();

        //是否删除
        strictInsertFill(metaObject, IS_DEL, Integer.class, Enums.DeleteStatus.NO_DELETE.code);
        //是否已读
        strictInsertFill(metaObject, IS_READ, Integer.class, Enums.ReadStatus.NO_READ.code);
        //是否确认
        strictInsertFill(metaObject, IS_CONFIRM, Integer.class, Enums.ConfirmStatus.NO_CONFIRM.code);
        //创建时间
        strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, now);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时间
        strictUpdateFill(metaObject, UPDATE_DATE, LocalDateTime.class, com.ptone.messageCenter.common.utils.TimeUtil.getNow());
    }
}